package com.group.Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.group.pojo.GEvaluate;
import com.group.pojo.GWork;

/**
 * Servlet implementation class WorkInfoServlet
 */
@WebServlet("/WorkInfoServlet")
public class WorkInfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public WorkInfoServlet() {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取请求动作
		String action = request.getParameter("action");
		
		//获取任务信息列表
		if(action.equals("info")) {
			GWork work = new GWork();
			List<GWork> work_list = work.getGWorkList();
			request.setAttribute("workList", work_list);
			request.getRequestDispatcher("tasks.jsp").forward(request, response);
		}
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取请求动作
		String action = request.getParameter("action");
		
		//获取任务信息列表
		if(action.equals("add")) {
			String workTitle = request.getParameter("workTitle");
			String workContent = request.getParameter("workContent");
			String workEnd = request.getParameter("endTime");
			String[] groupID = request.getParameterValues("groupID");
			GWork w = new GWork(workTitle, workContent, workEnd);
			w.addWork(w, groupID);
			response.sendRedirect("WorkInfoServlet?action=info");
		}
		else if(action.equals("getInfo")) {
			String workID = request.getParameter("workID");
			GWork w = new GWork();
			w = w.getGWorkInfoByID(Integer.parseInt(workID));
			Gson gson = new Gson();
			String jsonObj = gson.toJson(w);
			response.setHeader("Cache-Control", "no-cache");// 去除缓存
			response.setContentType("application/json;charset=utf-8");
			PrintWriter out = response.getWriter();
			out.print(jsonObj);
			out.flush();
			out.close();
		}
		else if(action.equals("edit")) {
			String workID = request.getParameter("workID");
			String workTitle = request.getParameter("workTitle");
			String workContent = request.getParameter("workContent");
			String workEnd = request.getParameter("endTime");
			String[] groupID = request.getParameterValues("groupID");
			GWork w = new GWork(workTitle, workContent, workEnd);
			w.setWorkID(Integer.parseInt(workID));
			w.updateWorkInfo(w, groupID);
			response.sendRedirect("WorkInfoServlet?action=info");
		}
		else if(action.equals("del")) {
			String workID = request.getParameter("workID");
			GWork w = new GWork();
			w.deleteWorkByID(Integer.parseInt(workID));
			response.sendRedirect("WorkInfoServlet?action=info");
		}
	}

}
